package com.ecjtu.stadiumre_servations.controller;

import com.ecjtu.stadiumre_servations.response.Result;
import com.ecjtu.stadiumre_servations.service.MinioService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;

@RestController
@RequestMapping("/minio")
public class MinioController {

    @Autowired
    private MinioService minioService;

    // 初始化存储桶
    @PostMapping("/init")
    public Result<Void> initBucket() {
        return minioService.initBucket();
    }

    // 上传文件
    // 上传文件
    @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Result<String> uploadFile(@RequestParam("file") MultipartFile file) {
        if (file == null) {
            return Result.fail(201, "图片上传失败: 文件不能为空");
        }
        return minioService.uploadFile(file);
    }

    // 下载文件
    @GetMapping("/download/{objectName}")
    public Result<InputStream> downloadFile(@PathVariable String objectName) {
        return minioService.downloadFile(objectName);
    }

    // 获取文件URL
    @GetMapping("/url/{objectName}")
    public Result<String> getFileUrl(@PathVariable String objectName) {
        return minioService.getFileUrl(objectName);
    }

    // 删除文件
    @DeleteMapping("/delete/{objectName}")
    public Result<Void> deleteFile(@PathVariable String objectName) {
        return minioService.deleteFile(objectName);
    }
    // 获取图片列表
    @GetMapping("/list")
    public Result<List<String>> listImages() {
        return minioService.listImages();
    }
}